package b2.widget.task
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.text.*;
	
	public class TaskDialog extends Sprite
	{
		public var taskDetailPane:TaskDetailPane;
		public var currentSelectedTaskItem:TaskItem;
		//左侧任务列表
		private var taskListPane:Tab;
		//同时进行的任务个数（主线任务1个，支线任务3个， 节日任务2个）
		private const CC_TASK_NUM:Array = [1, 3, 2];
		//帮助容器
		private var guideContainter:Sprite;
		//帮助步骤
		private var guideStep:int;
		
		private static var instance:TaskDialog;
		
		public function TaskDialog()
		{
			super();
			//背景图片
			var TaskDialogBgr:Class = ResourceHelper.getInstance().getClassByName("TaskDialogBgr");
			var taskDialogBgr:Sprite = new TaskDialogBgr();
			addChild(taskDialogBgr);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	var titleFomat:TextFormat = new TextFormat("微软雅黑", 16, 0xFFFFFF);
        	titleFomat.letterSpacing = 10;
			titleText.defaultTextFormat = titleFomat;
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.text = "军部大厅";
			titleText.x = (this.width - titleText.width) / 2;
			titleText.y = 16;
			addChild(titleText);
			//左侧任务列表
			var taskListText:TextField = new TextField();
			taskListText.selectable = false;
        	taskListText.autoSize = TextFieldAutoSize.LEFT;
			taskListText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFFFF);
			taskListText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			taskListText.text = "任务列表";
			taskListText.x = 95;
			taskListText.y = 74;
			addChild(taskListText);
			taskListPane = new Tab("", 1, 6);
			taskListPane.hidePageSelector();
			taskListPane.setPagePos(22, 120);
			addChild(taskListPane);
			//右侧任务详情
			taskDetailPane = new TaskDetailPane(this);
			taskDetailPane.x = 260;
			taskDetailPane.y = 50;
			addChild(taskDetailPane);
			//关闭按钮
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton");
			var closeButton:BasicButton = new BasicButton(CloseButton);
			closeButton.x = this.width - 48;
			closeButton.y = 16;
			addChild(closeButton);
			closeButton.addEventListener(MouseEvent.CLICK, onCloseButtonClick);
			this.guideStep = 2; 
		}
		
		public static function getInstance():TaskDialog
		{
			if (!instance)
				instance = new TaskDialog();
			return instance;
		}
		
		public function refresh():void
		{
			var taskSelectedFlag:Boolean = false;
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			taskListPane.removeAll();
			for (var taskType:int = 0; taskType < 3; taskType++)
			{
				//获取正在进行的任务
				var taskIndexes:Array = me.taskPackage.getOnGoingTasks(taskType);
				var nextTaskNum:int = CC_TASK_NUM[taskType] - taskIndexes.length;
				//尚未进行的任务
				if (nextTaskNum > 0)
				{
					for (var i:int = 0; i < Config.taskList.length; i++)
					{
						var taskConfig:TaskConfig = Config.taskConfigs[Config.taskList[i]];
						if (taskConfig.type == taskType && (taskConfig.requireTaskIndex == 0 || me.getTaskStatus(taskConfig.requireTaskIndex) == 2))
						{
							var taskStatus:int = me.getTaskStatus(taskConfig.index);
							if (taskStatus < 0)
							{
								taskIndexes.push(taskConfig.index);
								nextTaskNum--;
								if (nextTaskNum <= 0)
									break;
							}
						}
					}
				}
				//显示所有任务
				for (i = 0; i < taskIndexes.length; i++)
				{ 
					var taskItem:TaskItem = new TaskItem(taskIndexes[i]);
					taskItem.addEventListener(MouseEvent.CLICK, onTaskItemClick);
					taskListPane.addItem(taskItem, 169, 53);
					if (currentSelectedTaskItem != null && taskItem.taskIndex == currentSelectedTaskItem.taskIndex)
					{
						currentSelectedTaskItem = taskItem;
						taskSelectedFlag = true;
					}
				}
			}
			if (taskListPane.items.length > 0)
			{
				if (!taskSelectedFlag)
					currentSelectedTaskItem = taskListPane.items[0];
			}
			else
			{
				currentSelectedTaskItem = null;
			}
			if (currentSelectedTaskItem != null)
				currentSelectedTaskItem.selected = true;
			taskDetailPane.refresh();
		}
		
		public function select(taskIndex:int):void
		{
			for (var i:int = 0; i < taskListPane.items.length; i++)
			{
				var taskItem:TaskItem = taskListPane.items[i];
				if (taskItem.taskIndex == taskIndex && taskItem != currentSelectedTaskItem)
				{
					currentSelectedTaskItem.selected = false;
					taskItem.selected = true;
					currentSelectedTaskItem = taskItem;
					break;
				}
			}
		}
		
		public function close():void
		{
			if (this.parent != null)
			{
				this.parent.removeChild(this);
			}
		}
		
		public function startGuide():void
		{
			if (guideStep >= 2)
			{
				guideStep = 0;
				nextGuideStep();
			}
			else
			{
				guideStep = 0;
			}
		}
		
		private function nextGuideStep():void
		{
			if (guideContainter != null && guideContainter.parent != null)
				guideContainter.parent.removeChild(guideContainter);
			guideContainter = new Sprite();
			addChild(guideContainter);
			var blinkRect:BlinkRec;
			var bubbleMessage:BubbleMessage;
			if (guideStep == 0)
			{
				blinkRect = new BlinkRec(20, 112, 220, 240);
				bubbleMessage = new BubbleMessage("单击选择任务", true);
				bubbleMessage.x = 170;
				bubbleMessage.y = 63;
			}
			else if (guideStep == 1)
			{
				blinkRect = new BlinkRec(269, 238, 233, 27);
				bubbleMessage = new BubbleMessage("任务具体要求");
				bubbleMessage.x = 240;
				bubbleMessage.y = 188;
				var blinkRect2:BlinkRec = new BlinkRec(514, 238, 38, 27);
				var bubbleMessage2:BubbleMessage = new BubbleMessage("当前进度");
				bubbleMessage2.x = 400;
				bubbleMessage2.y = 188;
				guideContainter.addChild(blinkRect2);
				guideContainter.addChild(bubbleMessage2);
			}
			else
			{
				blinkRect = new BlinkRec(364, 446, 105, 44);
				bubbleMessage = new BubbleMessage("点击开始任务", true);
				bubbleMessage.x = 411;
				bubbleMessage.y = 396;
			}
			guideContainter.addChild(blinkRect);
			guideContainter.addChild(bubbleMessage);
			if (guideStep++ < 2)
				TweenLite.delayedCall(5, nextGuideStep);
		}
		
		private function onCloseButtonClick(event:MouseEvent):void
		{
			close();
		}
		
		private function onTaskItemClick(event:MouseEvent):void
		{
			var taskItem:TaskItem = event.currentTarget as TaskItem;
			if (currentSelectedTaskItem != taskItem)
			{
				currentSelectedTaskItem.selected = false;
				taskItem.selected = true;
				currentSelectedTaskItem = taskItem;
				taskDetailPane.refresh();
			}
		}
	}
}